/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import * as path from 'path';
import { OHPackageConfig } from '../../../src/tools/model/OHPackageConfig';

console.log('--- Demonstrating Module-level Config Creation ---');

// 1. Create a new module-level config from scratch
const moduleConfig = new OHPackageConfig({
    name: '@my-group/my-awesome-module',
    version: '1.0.0',
    license: 'Apache-2.0',
    main: 'index.ets',
});

// 2. Set various fields
moduleConfig.setDescription('An awesome module that does amazing things.');
moduleConfig.setAuthor({ name: 'John Doe', email: 'john.doe@example.com' });

// 3. Add dependencies
moduleConfig.addDependency('@ohos/utils', '^1.0.0', 'dependencies');
moduleConfig.addDependency('my-dev-tool', 'file:../tools/my-dev-tool', 'devDependencies');

console.log('Initial module config created:', moduleConfig.getRawConfig());

// 4. Save the new configuration to a file
const moduleConfigPath = path.join("test/resources/oh-package-json-examples", 'module-oh-package.json5');
moduleConfig.save(moduleConfigPath);
console.log(`Module config saved to: ${moduleConfigPath}`);

console.log('\n--- Demonstrating Project-level Config Reading and Modification ---');

// 5. Read an existing project-level config from a file
const projectConfigPath = path.join("test/resources/oh-package-json-examples", 'project-oh-package.json5');
const projectConfig = OHPackageConfig.fromFile(projectConfigPath);

console.log('Original project config loaded:', projectConfig.getRawConfig());

// 6. Update a field and add a new dependency
projectConfig.setDescription('This project config has been updated!');
projectConfig.addDependency('@ohos/new-feature', '~2.1.0', 'dependencies');

console.log('Modified project config:', projectConfig.getRawConfig());

// 7. Save the modified configuration back to a new file
const modifiedProjectConfigPath = path.join(__dirname, 'project-oh-package-modified.json5');
projectConfig.save(modifiedProjectConfigPath);
console.log(`Modified project config saved to: ${modifiedProjectConfigPath}`);

console.log('\n--- Verification ---');
// Verify by reading the saved files back
const savedModuleConfig = OHPackageConfig.fromFile(moduleConfigPath);
const savedModifiedProjectConfig = OHPackageConfig.fromFile(modifiedProjectConfigPath);

console.log('Read back module config:', savedModuleConfig.getRawConfig());
console.log('Read back modified project config:', savedModifiedProjectConfig.getRawConfig());
